package com.shxc.smarttrade.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;

import com.shxc.smarttrade.MyApplication;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.dialog.LoadingDialog;
import com.shxc.smarttrade.old.utils.CloseAllActivity;
import com.shxc.smarttrade.utils.sp.SpImp;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.x;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 基类Activity
 */
public abstract class BaseActivity extends AppCompatActivity {
    public Resources resource;
    public String pkgName;
    public Bundle bundle;
    public SpImp spImp;
    public Context mContext;

    public MyApplication myApplication;


    //    public IsSubmitDialog isSubmitDialog;
    public LoadingDialog loadingDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        hide();
        CloseAllActivity.getScreenManager().pushActivity(this);

        x.view().inject(this);
//        VirtualKeyAdaptation.assistActivity(findViewById(android.R.id.content));
        resource = this.getResources();
        pkgName = this.getPackageName();
        bundle = new Bundle();
        spImp = new SpImp(getApplicationContext());
        mContext = x.app().getApplicationContext();
        myApplication = new MyApplication();

        initDialog();

        super.onCreate(savedInstanceState);
    }

    protected void initDialog() {
//        isSubmitDialog = new IsSubmitDialog(this,
//                resource.getIdentifier("BottomDialog", "style", getPackageName()));
//        isSubmitDialog.getWindow().setGravity(Gravity.BOTTOM | Gravity.LEFT | Gravity.RIGHT);
        loadingDialog = new LoadingDialog(this, getResources().getIdentifier("MyDialog", "style", getPackageName()));
    }

    @SuppressLint("SimpleDateFormat")
    public String getData() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        return str;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        CloseAllActivity.getScreenManager().popActivity(this);
//        if (isSubmitDialog.isShowing()) {
//            isSubmitDialog.dismiss();
//        }
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    public void JumpTo(Class cla) {
        Intent intent = new Intent(this, cla);
        startActivity(intent);
    }

    public void JumpTo(Class cla, Bundle b) {
        Intent intent = new Intent(this, cla);
        intent.putExtras(b);
        startActivity(intent);
    }

    //隐藏状态栏
    public void hide() {
//        Window window = getWindow();
        //定义全屏参数
        requestWindowFeature(Window.FEATURE_NO_TITLE);

//        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
//        //设置当前窗体为全屏显示
//        window.setFlags(flag, flag);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            WindowManager.LayoutParams winParams = window.getAttributes();
//            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
//            winParams.flags |= bits;
//            window.setAttributes(winParams);
//        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransitionExit();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransitionEnter();
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransitionEnter();
    }

    /**
     * Overrides the pending Activity transition by performing the "Enter" animation.
     * 跳转到新activity时的演示动画
     */
    protected void overridePendingTransitionEnter() {
        overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
    }

    /**
     * Overrides the pending Activity transition by performing the "Exit" animation.
     * 关闭当前activity时的演示动画
     */
    protected void overridePendingTransitionExit() {
        overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
    }

}
